//
//  YZGradientBtn.m
//  VET
//
//  Created by Lam BG on 2018/6/28.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZGradientBtn.h"

#import "YZStatementHeader.h"

#import "UIColor+Category.h"
#import "UIImage+Gradient.h"

@interface YZGradientBtn()

@property (nonatomic, weak) UIView *coverView;

@property (nonatomic, weak) UIView *coverCountView;

@property (nonatomic, strong) dispatch_source_t timer;

@end

@implementation YZGradientBtn

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupUI];
    }
    
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    
    self = [super initWithCoder:aDecoder];
    if (self) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)dealloc {
    
    NSLog(@"YZGradientBtn dealloc");
}

- (void)setupUI {
    
    // 按钮背景渐变色
    UIImage *backImage = [UIImage gradientFillSize:self.frame.size withFirstColor:yzColorGS andSecondColor:yzColorGE];
    [self setBackgroundImage:backImage forState:UIControlStateNormal];
    self.clipsToBounds = YES;
    
    
    // 遮挡层
    UIView *cover = [[UIView alloc] initWithFrame:self.bounds];
    cover.backgroundColor = [UIColor whiteColor];
    cover.alpha = .5f;
    cover.hidden = YES;
    
    [self addSubview:cover];
    self.coverView = cover;
    
    // 倒计时遮挡
    UIView *coverCountView = [[UIView alloc] initWithFrame:self.bounds];
    coverCountView.backgroundColor = [UIColor clearColor];
    coverCountView.hidden = YES;
    
    [self addSubview:coverCountView];
    self.coverCountView = coverCountView;
    
    // 监听按钮的状态 切换遮挡层
    [self bindRAC];
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    self.coverView.frame = self.bounds;
}

- (void)bindRAC {
    
    @weakify(self)
    [[RACObserve(self, enabled) distinctUntilChanged] subscribeNext:^(id enabled) {
        
        @strongify(self)
        dispatch_async(dispatch_get_main_queue(), ^{
        
            BOOL isEnable = [enabled boolValue];
            self.coverView.hidden = isEnable;
        });
    }];
}

#pragma mark - 定时器 (GCD)

- (void)startCount:(NSInteger)countTime {
    
    // 显示隐藏遮罩
    self.coverView.hidden = YES;
    self.coverCountView.hidden = NO;
    
    if (self.timer != nil) {
        
        dispatch_source_cancel(self.timer);
        self.timer = nil;
    }
    
    // 设置倒计时时间
    __block NSInteger timeout = countTime;
    // 获取全局队列
    dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // 创建一个定时器，并将定时器的任务交给全局队列执行(并行，不会造成主线程阻塞)
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, global);
    // 设置触发的间隔时间
    dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    //设置定时器的触发事件
    dispatch_source_set_event_handler(self.timer, ^{

        // 每调用一次 时间-1s
        timeout --;
        // 对timeout进行判断时间是停止倒计时，还是修改button的title
        if (timeout <= 0) {
            
            //关闭定时器
            dispatch_source_cancel(self.timer);
            // 倒计时完成更新
            dispatch_async(dispatch_get_main_queue(), ^{
                
                NSString *countDoneInfo = @"获取验证码";
                self.titleLabel.text = countDoneInfo;
                [self setTitle:countDoneInfo forState:(UIControlStateNormal)];
                self.coverCountView.hidden = YES;
            });
        } else {
            
            //处于正在倒计时，在主线程中刷新button上的title，时间-1秒
            dispatch_async(dispatch_get_main_queue(), ^{
                
                NSString *countInfo = [NSString stringWithFormat:@"%ld", timeout];
                self.titleLabel.text = countInfo;
                [self setTitle:countInfo forState:(UIControlStateNormal)];
            });
        }
    });
    
    dispatch_resume(self.timer);
}


@end













